{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    1\na    2\nc    3\nd    4\ndtype: int64\n"
     ]
    }
   ],
   "source": [
    "s = pd.Series(data=[1, 2, 3, 4], index=[\"a\", \"a\", \"c\", \"d\"])\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    1\na    2\ndtype: int64\n[1 2 3 4]\nIndex(['a', 'a', 'c', 'd'], dtype='object')\n"
     ]
    }
   ],
   "source": [
    "print(s[\"a\"])\n",
    "print(s.values)\n",
    "print(s.index)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  name  age\n1   张三   23\n2   李四   24\n3   王五   25\n"
     ]
    }
   ],
   "source": [
    "data = {\n",
    "    \"name\": [\"张三\", \"李四\", \"王五\"],\n",
    "    \"age\": [23, 24, 25]\n",
    "}\n",
    "df = pd.DataFrame(data=data, index=[1, 2, 3])\n",
    "print(df)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   age\n1   23\n2   24\n3   25\n  name  age\n1   张三   23\n2   李四   24\n3   王五   25\n"
     ]
    }
   ],
   "source": [
    "print(df.drop(\"name\",axis=1))\n",
    "# del df[\"name\"]\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1    23\n2    24\n3    25\nName: age, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "print(df[\"age\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  name  age\n3   王五   25\n"
     ]
    }
   ],
   "source": [
    "print(df[df[\"age\"] > 24])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  name  age\n1   张三   23\n"
     ]
    }
   ],
   "source": [
    "print(df[df[\"name\"] == \"张三\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name    3\nage     3\ndtype: int64\n"
     ]
    }
   ],
   "source": [
    "print(df.count(axis=0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "        age\ncount   3.0\nmean   24.0\nstd     1.0\nmin    23.0\n25%    23.5\n50%    24.0\n75%    24.5\nmax    25.0\n"
     ]
    }
   ],
   "source": [
    "print(df.describe())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "72\n"
     ]
    }
   ],
   "source": [
    "print(df[\"age\"].sum())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             id name  age gender clazz\n0    1500100001  施笑槐   22      女  文科六班\n1    1500100002  吕金鹏   24      男  文科六班\n2    1500100003  单乐蕊   22      女  理科六班\n3    1500100004  葛德曜   24      男  理科三班\n4    1500100005  宣谷芹   22      女  理科五班\n..          ...  ...  ...    ...   ...\n995  1500100989  柏盼香   24      女  理科六班\n996  1500100990  扈旭鹏   23      男  理科三班\n997  1500100991  冉飞昂   22      男  理科一班\n998  1500100992  莫运盛   24      男  理科六班\n999  1500100993  衡从蕾   21      女  理科二班\n\n[1000 rows x 5 columns]\n"
     ]
    }
   ],
   "source": [
    "student = pd.read_csv(\"python/code/students.txt\", sep=\",\",\n",
    "                      header=None,\n",
    "                      encoding=\"utf-8\",\n",
    "                      names=[\"id\", \"name\", \"age\", \"gender\", \"clazz\"])\n",
    "print(student)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             id name  age gender clazz\n61   1500100055  卫鸿熙   24      男  文科一班\n124  1500100118  蔺昆宇   21      男  文科一班\n131  1500100125  蒙涵衍   23      男  文科一班\n134  1500100128  巫鸿哲   24      男  文科一班\n148  1500100142  闵华晖   21      男  文科一班\n171  1500100165  姜昆皓   22      男  文科一班\n282  1500100276  庾运鹏   24      男  文科一班\n345  1500100339  终胤运   23      男  文科一班\n348  1500100342  米昊明   21      男  文科一班\n382  1500100376  庾胤运   21      男  文科一班\n413  1500100407  束昊磊   21      男  文科一班\n448  1500100442  郎泽洋   23      男  文科一班\n462  1500100456  鄂运凯   24      男  文科一班\n542  1500100536  栾昊苍   23      男  文科一班\n543  1500100537  茹高旻   22      男  文科一班\n584  1500100578  殷泽洋   24      男  文科一班\n591  1500100585  穆海超   21      男  文科一班\n599  1500100593  景越泽   24      男  文科一班\n608  1500100602  殳昌黎   23      男  文科一班\n646  1500100640  纪昌黎   23      男  文科一班\n652  1500100646  窦海阳   24      男  文科一班\n658  1500100652  农鸿晖   21      男  文科一班\n691  1500100685  施昆颉   23      男  文科一班\n715  1500100709  俞昂杰   23      男  文科一班\n730  1500100724  路星腾   21      男  文科一班\n841  1500100835  柯晨朗   22      男  文科一班\n855  1500100849  寿昊英   22      男  文科一班\n927  1500100921  林智阳   21      男  文科一班\n934  1500100928  戚昌盛   22      男  文科一班\n949  1500100943  许昌黎   21      男  文科一班\n957  1500100951  平彭泽   22      男  文科一班\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Program Files\\python3\\lib\\site-packages\\ipykernel_launcher.py:1: UserWarning: Boolean Series key will be reindexed to match DataFrame index.\n  \"\"\"Entry point for launching an IPython kernel.\n"
     ]
    }
   ],
   "source": [
    "print(student[student[\"clazz\"]==\"文科一班\"]\n",
    "                                            [student[\"gender\"]==\"男\"] )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "           id name  age gender clazz\n0  1500100001  施笑槐   22      女  文科六班\n1  1500100002  吕金鹏   24      男  文科六班\n2  1500100003  单乐蕊   22      女  理科六班\n3  1500100004  葛德曜   24      男  理科三班\n4  1500100005  宣谷芹   22      女  理科五班\n"
     ]
    }
   ],
   "source": [
    "print(student.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             id name  age gender clazz\n995  1500100989  柏盼香   24      女  理科六班\n996  1500100990  扈旭鹏   23      男  理科三班\n997  1500100991  冉飞昂   22      男  理科一班\n998  1500100992  莫运盛   24      男  理科六班\n999  1500100993  衡从蕾   21      女  理科二班\n"
     ]
    }
   ],
   "source": [
    "print(student.tail())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "           id name  age gender clazz\n2  1500100003  单乐蕊   22      女  理科六班\n3  1500100004  葛德曜   24      男  理科三班\n"
     ]
    }
   ],
   "source": [
    "print(student.take(indices=[2,3]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [],
   "source": [
    "df2 = student[student[\"clazz\"]==\"文科一班\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [],
   "source": [
    "df2.to_csv(\"python/code/文科一班.txt\", header=None, index=False)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [],
   "source": [
    "df2.to_json(\"python/code/文科一班.json\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "clazz\n文科一班    22.416667\n文科三班    22.680851\n文科二班    22.379310\n文科五班    22.309524\n文科六班    22.605769\n文科四班    22.506173\n理科一班    22.333333\n理科三班    22.676471\n理科二班    22.556962\n理科五班    22.642857\n理科六班    22.489130\n理科四班    22.637363\nName: age, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "print(student.groupby(student[\"clazz\"]).mean()[\"age\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
